<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class Article_class extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->verify_user();
	}

	public function index()
	{
		$this->view('article_class');
	}

	public function data()
	{
		$this->db->where('pid', 0);
		$rs = $this->db->get('article_class');
		$arr = array();

		foreach ($rs->result_array() as $v) {
			$this->db->where('pid', $v['id']);
			$rs1 = $this->db->get('article_class');
			$v['children'] = $rs1->result_array();
			$arr[] = $v;
		}

		$result['rows'] = $arr;
		echo json_encode($result);
	}

	public function selectdata()
	{
		$pid = intval($this->uri->segment(3));
		$result = $this->db->where('pid', 0)->get('article_class')->result_array();
		$note[] = array('id' => 0, 'text' => '顶级分类');

		foreach ($result as $k => $v) {
			if ($pid == $v['id']) {
				$note[] = array('id' => $v['id'], 'text' => $v['name'], 'selected' => true);
			}
			else {
				$note[] = array('id' => $v['id'], 'text' => $v['name']);
			}
		}

		echo json_encode($note);
	}

	public function add()
	{
		$this->load->view('article_class_add');
	}

	public function insert()
	{
		$name = $this->input->post('name');
		$pid = $this->input->post('pid');
		$sort = $this->input->post('sort');
		$mod = $this->input->post('mod');
		$en_name = $this->input->post('en_name');
		$data = array('name' => $name, 'en_name' => $en_name, 'pid' => $pid, 'sort' => $sort, 'mod' => $mod);
		$rs = $this->db->insert('article_class', $data);

		if ($rs) {
			echo json_encode(array('msg' => '添加成功', 'error' => 0));
		}
		else {
			echo json_encode(array('msg' => '添加失败', 'error' => 1));
		}
	}

	public function edit($id)
	{
		$id = intval($id);
		$this->db->where('id', $id);
		$rs = $this->db->get('article_class');
		$d = $rs->row_array();
		$this->load->view('article_class_edit', $d);
	}

	public function update()
	{
		$id = $this->input->post('id');
		$name = $this->input->post('name');
		$pid = $this->input->post('pid');
		$sort = $this->input->post('sort');
		$mod = $this->input->post('mod');
		$en_name = $this->input->post('en_name');
		$data = array('name' => $name, 'en_name' => $en_name, 'pid' => $pid, 'sort' => $sort, 'mod' => $mod);
		$this->db->where('id', $id);
		$rs = $this->db->update('article_class', $data);

		if ($rs) {
			echo json_encode(array('msg' => '修改成功', 'error' => 0));
		}
		else {
			echo json_encode(array('msg' => '修改失败', 'error' => 1));
		}
	}

	public function del()
	{
		$id = $this->input->get('id');

		if ($id) {
			$this->db->where_in('id', $id);
			$rs = $this->db->delete('article_class');

			if ($rs) {
				echo json_encode(array('msg' => '删除成功', 'error' => 0));
			}
			else {
				echo json_encode(array('msg' => '删除失败', 'error' => 1));
			}
		}
		else {
			echo json_encode(array('msg' => '删除失败！至少选择一行数据', 'error' => 0));
		}
	}
}

?>
